package com.dps.omc.schedule;

import com.dps.omc.enumer.EnumJobState;
import com.dps.omc.model.Job;
import com.dps.omc.model.JobReportDate;
import com.dps.omc.service.IJobReportDateService;
import com.dps.omc.service.IJobService;
import com.dps.common.utils.DateUtils;
import com.dps.common.utils.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * Created by DXH on 2016/5/16.
 */
@Service
public class JobReportDateTask extends  BaseTask{

    @Autowired
    private IJobReportDateService jobReportDateService;
    @Autowired
    private IJobService jobService;

    public void execute() throws Exception {
        logInfo("JobReportDateTask任务开始");
        this.create();
        logInfo("JobReportDateTask任务结束");
    }

    private void create()
    {
        JobReportDate jobReportDate = new JobReportDate();
        Date reportDate = DateUtils.getSpecifiedDayBefore(DateUtils.getNowDate(),2);

        Date queryStart = reportDate;//创建指定开始日期
        Date queryEnd = DateUtils.getSpecifiedDayBefore(DateUtils.getNowDate(),1) ;

        //找出指定时间内的成功和失败的作业数
        Integer jobSucc = this.getJobCount(queryStart,queryEnd, EnumJobState.SUCC);
        Integer jobFail = this.getJobCount(queryStart,queryEnd,EnumJobState.FAIL);

        jobReportDate.setId(IdGenerator.createNewId());
        jobReportDate.setReportDate(reportDate);
        jobReportDate.setJobSucc(jobSucc);
        jobReportDate.setJobFail(jobFail);
        jobReportDateService.add(jobReportDate);
    }


    //获取指定时间范围的作业数
    private Integer getJobCount(Date queryStart, Date queryEnd, EnumJobState status)
    {
        List<Job> jobs = jobService.queryByCreateDateAndStatus(queryStart,queryEnd,status);
        return jobs.size();
    }


}
